<?php
namespace app\common\lib;

use think\Db;

class LeaveCheck{

    //请假初审、终审
    public static function leave_pass($auditId, $check_desc, $record_add_integral = 0, $first_add_integral = 0)
    {
        //判断需要审核任务的状态，并且判断截至时间
        $auditInfo = M('com_leave')->where('id', $auditId)->find();
        if(!$auditInfo)  return '数据不存在!';

        //判断当前人是否为该任务的，审核人
        $userId = session("user_com")['id'];

        switch ($auditInfo['status']) {
            case 0:         //待初审
                if($auditInfo['first_check_user_id'] != $userId)  return '你不是该任务相关初审人，不能审核';
                $saveData['first_check_desc'] = $check_desc;
                $saveData['first_check_time'] = time();
                break;
            case 1:         // 1待终审
                if($auditInfo['last_check_user_id'] != $userId)  return '你不是该任务相关终审人，不能审核';
                $saveData['last_check_desc'] = $check_desc;
                $saveData['last_check_time'] = time();
                break;
            default:
                return '审核，数据有误！';
                break;
        }

        $saveData['id']     = $auditId;
        $saveData['status'] = $auditInfo['status'] + 1;

        //审核通过
        DB::startTrans();//开启事务
        try{
            M('com_leave')->update($saveData);

            //终审通过
            if( $auditInfo['status'] == 1 ) {
                SendIntegralNew::send($auditId, 4);
            }

            DB::commit();//提交
        }catch (\Exception $e){
            DB::rollback();//回滚
            return $e->getMessage();
        }
        //通知
        $relation_id = $auditInfo['id'];
        //初审通过，后续事件
        if($auditInfo['status'] < 1) {
            $user_id = $auditInfo['last_check_user_id'];
            $title = '当前有一条请假审请需要您审核(终审)';
            \app\company\logic\Message::send_message($title, $user_id, 1, 1, $relation_id);
            //通知`last_check_user_id` '终审人',初审通过
        }

        if($auditInfo['status'] == 1){
            $user_id = $auditInfo['user_id'];
            $title = '您有一条请假审核已通过';
            \app\company\logic\Message::send_message($title, $user_id, 3, 4, $relation_id);
        }


    }
}
